<template lang="html">
  <div class="breadcrumb">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item v-for="(item, index) in pageCp.data" :key="item.path">
        <span
          v-if="item.disabledNavigateTo || index == pageCp.data.length - 1"
          class="disabledNavigate"
          >{{ item.meta.title }}</span
        >
        <!-- <span v-else @click.prevent="rNavigateTo(item)">{{ item.meta.title }}</span> -->
        <router-link v-else :to="{ name: item.name, params: {} }">{{
          item.meta.title
        }}</router-link>
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "breadcrumb",
  data: () => ({
    pageCp: {
      data: []
    }
  }),
  watch: {
    $route() {
      this.getBreadcrumb();
    }
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      // 只显示在meta有标题的
      let matched = this.$route.matched.filter(
        item => item.meta && item.meta.title
      );
      let firstMatched = matched[0];
      // console.log('firstMatched', firstMatched)
      // 不是首页，加上首页
      if (!this.isDashboard(firstMatched)) {
        // console.log('true')
        matched = [
          { path: "/dashboard", name: "home", meta: { title: "首页" } }
        ].concat(matched);
      }
      // const first = matched[0]
      // if (!this.isDashboard(first)) {
      //   matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
      // }
      // 只显示有标题且没声明隐藏-
      this.pageCp.data = matched.filter(
        item =>
          item.meta && item.meta.title && item.meta.showInBreadcrumb !== false
      );
      // console.log('this.pageCp.data', this.pageCp.data, matched)
    },
    rNavigateTo() {
      // const { path } = route;
      // console.log('path', path)
      this.$router.push("/dashboard");
    },
    isDashboard(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      // console.log('route', route, name);
      return name.trim().toLowerCase() === "dashboard";
    }
  }
};
</script>

<style lang="scss" scoped>
.breadcrumb {
  .el-breadcrumb__item {
    line-height: 50px;
  }
  .disabledNavigate {
    color: #ccc;
  }
  .router-link-active {
    color: #828282;
  }
}
</style>
